<template>
  <div class="app-container">

    <el-dialog
      title="修改食品信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="form">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分类编码" prop="size">
          <el-input v-model="form.code" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doUpdate(form)">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      v-loading="listLoading"
      :data="form"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column align="center" label="ID" width="95" prop="id" />
      <el-table-column label="分类名称" prop="name" width="140" />
      <el-table-column label="分类编码" width="140" align="center" prop="code" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="middle"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="middle"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>

import getClassOptions from '@/api/class'
import { updateClass } from '@/api/class'
import { deleteClass } from '@/api/class'

export default {
  data() {
    return {
      form: {
        name: '',
        code: '',
        id: ''
      },
      param: {},
      foodList: [],
      listLoading: true,
      dialogVisible: false
    }
  },

  created() {
    this.feachDate()
  },
  methods: {
    onSubmit() {
      this.$message('submit!')
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },

    feachDate() {
      const param = {}
      getClassOptions(param).then(res => {
        this.form = res.data
        console.log(res.data)
        console.log(this.form)
        this.listLoading = false
      })
    },
    handleDelete(data) {
      let { $message } = this
      deleteClass(data).then(res => {
        console.log(data, res)
        if (res.code === 100) {
          $message = '删除成功'
          this.feachDate()
        }
      })
    },
    handleUpdate(data) {
      this.form = data
      this.dialogVisible = true
    },
    doUpdate(data) {
      updateClass(data).then(res => {
        console.log(res)
        if (res.code === 100) {
          this.$message = '修改成功'
          this.dialogVisible = false
          this.feachDate()
        }
      })
    }

  }
}
</script>

<style scoped>
  .line {
    text-align: center;
  }
</style>

